package city.spring.modules.medicine.controller;

import city.spring.modules.medicine.entity.MedicineInventoryEntity;
import city.spring.modules.medicine.service.MedicineInventoryService;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.NonNull;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * Controller：药品库存盘点计划表
 *
 * @author 谢金婷
 * @date 2020-07-12 22:36:56
 */
@RestController
@RequestMapping("/medicineinventory")
public class MedicineinventoryController extends ApiController {
    private final MedicineInventoryService medicineinventoryService;
    private final UserService userService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<MedicineInventoryEntity, ?>> entityOrderFields;

    public MedicineinventoryController(MedicineInventoryService medicineinventoryService, UserService userService) {
        this.medicineinventoryService = medicineinventoryService;
        this.userService = userService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(MedicineInventoryEntity::getId);
//        entityOrderFields.add(MedicineInventoryEntity::getMedicineId);
//        entityOrderFields.add(MedicineInventoryEntity::getInventoryquantity);
//        entityOrderFields.add(MedicineInventoryEntity::getProfitAndLoss);
//        entityOrderFields.add(MedicineInventoryEntity::getUserId);
//        entityOrderFields.add(MedicineInventoryEntity::getInventorytime);
        entityOrderFields.add(MedicineInventoryEntity::getGmtCreate);
        entityOrderFields.add(MedicineInventoryEntity::getGmtModified);
//        entityOrderFields.add(MedicineInventoryEntity::getStorehouseId);
//        entityOrderFields.add(MedicineInventoryEntity::getShelfId);
//        entityOrderFields.add(MedicineInventoryEntity::getQuantity);
    }

    /**
     * 获取全部的 <strong>药品库存盘点计划表</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, MedicineInventoryEntity entity) {
        LambdaQueryChainWrapper<MedicineInventoryEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>药品库存盘点计划表</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, MedicineInventoryEntity entity) {
        LambdaQueryChainWrapper<MedicineInventoryEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<MedicineInventoryEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        for (MedicineInventoryEntity record : page.getRecords()) {
            try {
                UserEntity userEntity = userService.getById(record.getFounder());
                record.setNickname(userEntity.getNickname());
            } catch (Exception ignore) {
                record.setFounder(null);
                record.setNickname("< 找不到创建人员 >");
            }
        }
        return success(page);
    }

    /**
     * 获取一个 <strong>药品库存盘点计划表</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(medicineinventoryService.getById(id));
    }

    /**
     * 添加一个 <strong>药品库存盘点计划表</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody MedicineInventoryEntity entity, @NonNull Principal user) {
        medicineinventoryService.saveMedicineinventory(entity, user);

        return success(entity);
//        HttpHeaders headers = new HttpHeaders();
//        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
//        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>药品库存盘点计划表</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody MedicineInventoryEntity entity) {
        entity.setId(id);
        medicineinventoryService.updateMedicineinventory(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>药品库存盘点计划表</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        medicineinventoryService.deleteMedicineinventory(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>药品库存盘点计划表</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        medicineinventoryService.deleteMedicineinventory(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<MedicineInventoryEntity> buildLambdaQuery(MedicineInventoryEntity entity) {
        LambdaQueryChainWrapper<MedicineInventoryEntity> lambdaQuery = medicineinventoryService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), MedicineInventoryEntity::getId, entity.getId());
//        lambdaQuery.like(StringUtils.isNotBlank(entity.getMedicineId()), MedicineInventoryEntity::getMedicineId, entity.getMedicineId());
//        lambdaQuery.eq(entity.getInventoryquantity() != null, MedicineInventoryEntity::getInventoryquantity, entity.getInventoryquantity());
//        lambdaQuery.eq(entity.getProfitAndLoss() != null, MedicineInventoryEntity::getProfitAndLoss, entity.getProfitAndLoss());
//        lambdaQuery.like(StringUtils.isNotBlank(entity.getUserId()), MedicineInventoryEntity::getUserId, entity.getUserId());
//        lambdaQuery.eq(entity.getInventorytime() != null, MedicineInventoryEntity::getInventorytime, entity.getInventorytime());
        lambdaQuery.eq(entity.getGmtCreate() != null, MedicineInventoryEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, MedicineInventoryEntity::getGmtModified, entity.getGmtModified());
//        lambdaQuery.like(StringUtils.isNotBlank(entity.getStorehouseId()), MedicineInventoryEntity::getStorehouseId, entity.getStorehouseId());
//        lambdaQuery.like(StringUtils.isNotBlank(entity.getShelfId()), MedicineInventoryEntity::getShelfId, entity.getShelfId());
//        lambdaQuery.eq(entity.getQuantity() != null, MedicineInventoryEntity::getQuantity, entity.getQuantity());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<MedicineInventoryEntity> buildQuery(MedicineInventoryEntity entity) {
        QueryWrapper<MedicineInventoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
//        wrapper.like(StringUtils.isNotBlank(entity.getMedicineId()), "medicine_id", entity.getMedicineId());
//        wrapper.eq(entity.getInventoryquantity() != null, "inventoryquantity", entity.getInventoryquantity());
//        wrapper.eq(entity.getProfitAndLoss() != null, "profit_and_loss", entity.getProfitAndLoss());
//        wrapper.like(StringUtils.isNotBlank(entity.getUserId()), "user_id", entity.getUserId());
//        wrapper.eq(entity.getInventorytime() != null, "inventorytime", entity.getInventorytime());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
//        wrapper.like(StringUtils.isNotBlank(entity.getStorehouseId()), "storehouse_id", entity.getStorehouseId());
//        wrapper.like(StringUtils.isNotBlank(entity.getShelfId()), "shelf_id", entity.getShelfId());
//        wrapper.eq(entity.getQuantity() != null, "quantity", entity.getQuantity());
        return wrapper;
    }

}
